

package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.lq.ht.dao.EmployeeDao;
import org.lq.ht.entity.Employee;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 员工表
 * @author 
 * @phone 15936587261
 *
 * @package org.lq.ht.dao
 * @date 2020年8月20日下午10:38:55
 */
public class EmployeeDaoImpl implements EmployeeDao,Instantiation<Employee> {

	@Override
	public int save(Employee t) {
		try {
			String sql = "insert into employee(employee_num,"
					+ "user_name,"
					+ "password,"
					+ "phone_number,position,note,name,salary,hiredate,icon)values(?,?,?,?,?,?,?,?,now(),?)";
			return JDBCUtil.executeUpdate(sql, t.getEmployeeNum(),t.getUserName(),
					t.getPassword(),t.getPhoneNumber(),t.getPosition(),t.getNote(),
					t.getName(),t.getSalary(),t.getIcon());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int update(Employee t) {
		String sql = "update  employee set employee_num = ?,user_name = ?,password = ?,phone_number = ?, "
				+ "position = ?,note = ? ,name=?,salary=?,hiredate=now(),icon=? where no = ?";
		try {
			return JDBCUtil.executeUpdate(sql, 
					t.getEmployeeNum(),
					t.getUserName(),
					t.getPassword(),
					t.getPassword(),
					t.getPosition(),
					t.getNote(),
					t.getNo(),
					t.getName(),t.getSalary(),t.getIcon()
					);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int delete(int id) {
		try {
			return JDBCUtil.executeUpdate("delete from employee where no = ?", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<Employee> findAll() {
		try {
			return JDBCUtil.executeQuery("select *from employee", this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Employee getById(int id) {
		try {
			List<Employee> list = JDBCUtil.executeQuery("select *from employee where no = ?",this,id);
			return list.get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Employee instance(ResultSet rs) {
		Employee emp = new Employee() ;
		emp = new Employee();
		try {
			emp.setNo(rs.getInt("no"));
			emp.setEmployeeNum(rs.getInt("employee_num"));
			emp.setUserName(rs.getString("user_name"));
			emp.setPassword(rs.getString("password"));
			emp.setPhoneNumber(rs.getString("phone_number"));
			emp.setPosition(rs.getString("position"));
			emp.setNote(rs.getString("note"));
			emp.setName(rs.getString("name"));
			emp.setSalary(rs.getInt("salary"));
			emp.setHiredate(rs.getTimestamp("hiredate"));
			emp.setIcon(rs.getString("icon"));
			return emp;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

